﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
//using www.SService;
using Controllerss;
using Core.Contract.DataContract;

namespace www.Admin.ucControl
{
    public partial class Sidebars : System.Web.UI.UserControl
    {
        #region Value
        private readonly ObjectCache _cache = MemoryCache.Default;
        //private readonly HyServicesClient _service1 = new HyServicesClient();
        private readonly MenuController _menuController=new MenuController();
        private readonly MenuMemberController _menuMemberController=new MenuMemberController();
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (_cache["Menu"] == null)
            {
                var lData = _menuController.GetMenuDataContracts();
                if (lData.Any())
                {
                    var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
                    _cache.Set("Menu", lData.ToList(), policy);
                }
            }
            if (_cache["StaffMenu"] == null)
            {
                var lData = _menuMemberController.GetMenuMemberDataContracts();
                if (lData.Any())
                {
                    var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
                    _cache.Set("StaffMenu", lData.ToList(), policy);
                }
            }
        }

        #region Business

        protected string LoadMenu()
        {
            var mul = "";
            var lMenu = (List<RelaxMenuDataContract>)_cache["Menu"];
            var lStaffMenu = (List<RelaxMenuMemberDataContract>)_cache["StaffMenu"];
            if (lMenu == null || lStaffMenu == null)
                return null;
            var permistion = (from m in lMenu.Where(p => p.Active == 1 && p.Languagecode == 1)
                              join s in lStaffMenu.Where(p => p.MemberId == Convert.ToInt32(Session["StaffId"]) && p.Active == 1) on m.Id equals
                                  s.MenuId
                              select new RelaxMenuDataContract
                              {
                                  Id = m.Id,
                                  Name = m.Name,
                                  Tag = m.Tag,
                                  Keyword = m.Keyword,
                                  Decription = m.Decription,
                                  Parent = m.Parent,
                                  CategoryId = m.CategoryId,
                                  Type = m.Type,
                                  Link = m.Link,
                                  Target = m.Target,
                                  Ord = m.Ord,
                                  Active = m.Active
                              }).ToList();
            if (permistion.Any())
            {
                mul = "<ul class=\"modul\">";
                foreach (var item in permistion)
                {
                    mul += "<li><a href='../" + item.Link + "' title='" + item.Name + "' target='" + item.Target + "'>" + item.Name + "</a></li>";
                }
                mul += "</ul>";
            }
            return mul;
        }

        #endregion
    }
}